//
//  AllCommentsViewController.swift
//  iModern
//
//  Created by 立雄 王 on 15/8/28.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit

class AllCommentsViewController: ViewController, UITableViewDelegate, UITableViewDataSource
{
    
    var tableView:         UITableView!
    let screen          = UIScreen.mainScreen().bounds
    
    var max            = 0
    var offset         = 0
    var end            = false
    var appDelegate    = UIApplication.sharedApplication().delegate as! AppDelegate
    let articleManager = ArticleManager()
    var articles       = NSMutableArray()
    
    let appointmentManager = AppointmentManager()
    var review       = NSMutableArray()
    var id = Int()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "全部评价"
        
        view.backgroundColor = UIColor(white: 0.98, alpha: 1)
        let frame = CGRect(x: 0, y: 0, width: screen.width, height: screen.height - 65)
        tableView = UITableView(frame: frame)
        view.addSubview(tableView)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.backgroundColor = UIColor.whiteColor()
        tableView.separatorStyle = .None
        
        self.tableView.registerNib(UINib(nibName: "AllCommentCellTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "ALLCOMMENT")
        
        self.loadData()
        initBackButton()
    }
    
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return review.count
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        //let screen = UIScreen.mainScreen().bounds
        
        //let message       = review.objectAtIndex(indexPath.row).objectForKey("review") as! NSString
        //let messageHeight = sizeOfText(message).height
        
        return 110
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let order = review[indexPath.row] as! NSMutableDictionary
        let cell = tableView.dequeueReusableCellWithIdentifier("ALLCOMMENT", forIndexPath: indexPath) as! AllCommentCellTableViewCell
        cell.selectionStyle = UITableViewCellSelectionStyle.None
        cell.setData(order)
        return cell
    }
    
    
    func sizeOfText(text: NSString) -> CGSize
    {
        let paragraphStyle = NSMutableParagraphStyle()
        
        paragraphStyle.lineHeightMultiple = 16
        paragraphStyle.maximumLineHeight = 20
        paragraphStyle.minimumLineHeight = 20
        
        //        paragraphStyle.firstLineHeadIndent = 20
        paragraphStyle.alignment = .Left
        
        let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(14),
            NSParagraphStyleAttributeName: paragraphStyle,
            NSForegroundColorAttributeName: UIColor.darkGrayColor()]
        
        let frame = CGSizeMake(screen.size.width, screen.size.height)
        let options = unsafeBitCast(NSStringDrawingOptions.TruncatesLastVisibleLine.rawValue|NSStringDrawingOptions.UsesLineFragmentOrigin.rawValue |
            NSStringDrawingOptions.UsesFontLeading.rawValue,
            NSStringDrawingOptions.self)
        let size  = text.boundingRectWithSize(frame, options: options, attributes: attributes, context: nil)
        return size.size
    }
    
    func loadData()
    {
        if (end)
        {
            return;
        }
        var rs: ResultSet?
        let ids = String(id)
        
        rs = appointmentManager.queryReviewAppointment(ids, max: max, offset: offset)
        
        if (self.verifyResutSet(rs))
        {
            if (rs!.isEmpty())
            {
                end = true
            }
            else
            {
                review.addObjectsFromArray(rs!.toMutableArray() as [AnyObject])
                offset = review.count
            }
        }
        
        self.tableView.reloadData()
    }
    
}

